﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Collections.Generic;//Please add references
namespace EnterpriseFund.DAL
{
    /// <summary>
    /// 数据访问类:AllowanceIssueEntry
    /// </summary>
    public partial class AllowanceIssueEntry
    {
        public AllowanceIssueEntry()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(decimal FAllowanceIssueEntryID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_AllowanceIssueEntry");
            strSql.Append(" where FAllowanceIssueEntryID=@FAllowanceIssueEntryID");
            SqlParameter[] parameters = {
					new SqlParameter("@FAllowanceIssueEntryID", SqlDbType.Decimal)
			};
            parameters[0].Value = FAllowanceIssueEntryID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public decimal Add(EnterpriseFund.Model.AllowanceIssueEntry model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_AllowanceIssueEntry(");
            strSql.Append("FIssueID,FYear,FTaxClassAllowanceNo,FAllowancePeriodAmt,FAllowanceBalAmt,NSRDZDAH,FAllowanceFileEntryID)");
            strSql.Append(" values (");
            strSql.Append("@FIssueID,@FYear,@FTaxClassAllowanceNo,@FAllowancePeriodAmt,@FAllowanceBalAmt,@NSRDZDAH,@FAllowanceFileEntryID)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@FIssueID", SqlDbType.NVarChar,50),
					new SqlParameter("@FYear", SqlDbType.Decimal,5),
					new SqlParameter("@FTaxClassAllowanceNo", SqlDbType.NVarChar,20),
					new SqlParameter("@FAllowancePeriodAmt", SqlDbType.Decimal,13),
					new SqlParameter("@FAllowanceBalAmt", SqlDbType.Decimal,13),
					new SqlParameter("@NSRDZDAH", SqlDbType.Decimal,13),
					new SqlParameter("@FAllowanceFileEntryID", SqlDbType.Decimal,13)};
            parameters[0].Value = model.FIssueID;
            parameters[1].Value = model.FYear;
            parameters[2].Value = model.FTaxClassAllowanceNo;
            parameters[3].Value = model.FAllowancePeriodAmt;
            parameters[4].Value = model.FAllowanceBalAmt;
            parameters[5].Value = model.NSRDZDAH;
            parameters[6].Value = model.FAllowanceFileEntryID;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToDecimal(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(EnterpriseFund.Model.AllowanceIssueEntry model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_AllowanceIssueEntry set ");
            strSql.Append("FAllowancePeriodAmt=@FAllowancePeriodAmt,");
            strSql.Append("FAllowanceBalAmt=@FAllowanceBalAmt");
            strSql.Append(" where FAllowanceIssueEntryID=@FAllowanceIssueEntryID");
            SqlParameter[] parameters = {
					new SqlParameter("@FAllowancePeriodAmt", SqlDbType.Decimal,13),
					new SqlParameter("@FAllowanceBalAmt", SqlDbType.Decimal,13),
					new SqlParameter("@FAllowanceIssueEntryID", SqlDbType.Decimal,13),
					new SqlParameter("@FIssueID", SqlDbType.NVarChar,50),
					new SqlParameter("@FYear", SqlDbType.Decimal,5),
					new SqlParameter("@FTaxClassAllowanceNo", SqlDbType.NVarChar,20),
					new SqlParameter("@NSRDZDAH", SqlDbType.Decimal,13),
					new SqlParameter("@FAllowanceFileEntryID", SqlDbType.Decimal,13)};
            parameters[0].Value = model.FAllowancePeriodAmt;
            parameters[1].Value = model.FAllowanceBalAmt;
            parameters[2].Value = model.FAllowanceIssueEntryID;
            parameters[3].Value = model.FIssueID;
            parameters[4].Value = model.FYear;
            parameters[5].Value = model.FTaxClassAllowanceNo;
            parameters[6].Value = model.NSRDZDAH;
            parameters[7].Value = model.FAllowanceFileEntryID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(decimal FAllowanceIssueEntryID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_AllowanceIssueEntry ");
            strSql.Append(" where FAllowanceIssueEntryID=@FAllowanceIssueEntryID");
            SqlParameter[] parameters = {
					new SqlParameter("@FAllowanceIssueEntryID", SqlDbType.Decimal)
			};
            parameters[0].Value = FAllowanceIssueEntryID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string FAllowanceIssueEntryIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_AllowanceIssueEntry ");
            strSql.Append(" where FAllowanceIssueEntryID in (" + FAllowanceIssueEntryIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void Return(Model.AllowanceIssueEntry model)
        {
            StringBuilder strSql = new StringBuilder();
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            cmds = new CommandInfo();
            strSql = new StringBuilder();
            strSql.Append("update fund.t_AllowanceIssueEntry set ");
            strSql.Append("FStatus=@FStatus ");
            strSql.Append(" where FIssueID=@FIssueID ");
            SqlParameter[] parameters4 = {
					new SqlParameter("@FStatus", SqlDbType.NVarChar,6),
					new SqlParameter("@FIssueID", SqlDbType.NVarChar,50)};
            parameters4[0].Value = model.FStatus;
            parameters4[1].Value = model.FIssueID;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters4;
            cmdList.Add(cmds);
            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithString(cmdList);
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public EnterpriseFund.Model.AllowanceIssueEntry GetModel(decimal FAllowanceIssueEntryID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FAllowanceIssueEntryID,FIssueID,FYear,FTaxClassAllowanceNo,FAllowancePeriodAmt,FAllowanceBalAmt,NSRDZDAH,FAllowanceFileEntryID from fund.t_AllowanceIssueEntry ");
            strSql.Append(" where FAllowanceIssueEntryID=@FAllowanceIssueEntryID");
            SqlParameter[] parameters = {
					new SqlParameter("@FAllowanceIssueEntryID", SqlDbType.Decimal)
			};
            parameters[0].Value = FAllowanceIssueEntryID;

            EnterpriseFund.Model.AllowanceIssueEntry model = new EnterpriseFund.Model.AllowanceIssueEntry();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public EnterpriseFund.Model.AllowanceIssueEntry DataRowToModel(DataRow row)
        {
            EnterpriseFund.Model.AllowanceIssueEntry model = new EnterpriseFund.Model.AllowanceIssueEntry();
            if (row != null)
            {
                if (row["FAllowanceIssueEntryID"] != null && row["FAllowanceIssueEntryID"].ToString() != "")
                {
                    model.FAllowanceIssueEntryID = decimal.Parse(row["FAllowanceIssueEntryID"].ToString());
                }
                if (row["FIssueID"] != null)
                {
                    model.FIssueID = row["FIssueID"].ToString();
                }
                if (row["FYear"] != null && row["FYear"].ToString() != "")
                {
                    model.FYear = decimal.Parse(row["FYear"].ToString());
                }
                if (row["FTaxClassAllowanceNo"] != null)
                {
                    model.FTaxClassAllowanceNo = row["FTaxClassAllowanceNo"].ToString();
                }
                if (row["FAllowancePeriodAmt"] != null && row["FAllowancePeriodAmt"].ToString() != "")
                {
                    model.FAllowancePeriodAmt = decimal.Parse(row["FAllowancePeriodAmt"].ToString());
                }
                if (row["FAllowanceBalAmt"] != null && row["FAllowanceBalAmt"].ToString() != "")
                {
                    model.FAllowanceBalAmt = decimal.Parse(row["FAllowanceBalAmt"].ToString());
                }
                if (row["NSRDZDAH"] != null && row["NSRDZDAH"].ToString() != "")
                {
                    model.NSRDZDAH = decimal.Parse(row["NSRDZDAH"].ToString());
                }
                if (row["FAllowanceFileEntryID"] != null && row["FAllowanceFileEntryID"].ToString() != "")
                {
                    model.FAllowanceFileEntryID = row["FAllowanceFileEntryID"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FAllowanceIssueEntryID,FIssueID,FYear,FTaxClassAllowanceNo,FAllowancePeriodAmt,FAllowanceBalAmt,NSRDZDAH,FAllowanceFileEntryID ");
            strSql.Append(" FROM fund.t_AllowanceIssueEntry ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetWCQKList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select FIssueID ,FStatus,COUNT(*) as counts from (
select NSRDZDAH,FIssueID,min(FStatus) as FStatus from fund.t_AllowanceIssueEntry 
group by NSRDZDAH,FIssueID) a ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" group by FIssueID,FStatus");
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllowanceFileList(string financePropNo, string issueIDList)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT a.*,b.NSRMC FROM (
SELECT NSRDZDAH,SUM(ISNULL(FFactAllowanceAmt,0)) AS je
  FROM [fund].[v_AllowanceIssueAllEntry] WHERE  FNewFinancePropNo ='" + financePropNo + @"' AND FIssueID IN (" + issueIDList + @") and FStatus='2' GROUP BY NSRDZDAH
  ) AS a LEFT JOIN ltais.DJ_NSRXX b ON a.NSRDZDAH = b.NSRDZDAH ");
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" FAllowanceIssueEntryID,FIssueID,FYear,FTaxClassAllowanceNo,FAllowancePeriodAmt,FAllowanceBalAmt,NSRDZDAH,FAllowanceFileEntryID ");
            strSql.Append(" FROM t_AllowanceIssueEntry ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM t_AllowanceIssueEntry ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.FAllowanceIssueEntryID desc");
            }
            strSql.Append(")AS Row, T.*  from t_AllowanceIssueEntry T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "t_AllowanceIssueEntry";
            parameters[1].Value = "FAllowanceIssueEntryID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

